package 代码记录.指定公司代码练习记录.西安.分值200分.分支200分未做;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.stream.IntStream;

/**
 * @author zx
 * @create 2023-07-11 21:08
 */
public class 最大相连男生数 {
    private static int maxLen;
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] split = reader.readLine().split(",");
        int n = Integer.parseInt(split[0]), m = Integer.parseInt(split[1]);
        char[][] matrix = new char[n][m];
        for (int i = 0; i < n; ++i) {
            matrix[i] = reader.readLine().replaceAll(",", "").toCharArray();
        }
        solveMethod(n, m, matrix);
    }

    private static void solveMethod(int n, int m, char[][] matrix) {
        final int[][] ACTIONS = {{0, 1}, {1, 0}, {1, 1}, {-1, -1}};
        IntStream.range(0, n - maxLen)
                .parallel()
                .forEach(i -> IntStream.range(0, m - maxLen)
                        .parallel()
                        .forEach(j -> Arrays.stream(ACTIONS)
                                .parallel()
                                .forEach(action -> find(matrix, action, i, j))));
        System.out.println(maxLen);
    }

    private static void find(char[][] matrix, int[] action, int r, int c) {
        int count = 0;
        while (r >= 0 && r < matrix.length && c >= 0 && c < matrix[0].length) {
            count = (matrix[r][c] == 'M') ? count + 1 : 0;
            maxLen = Math.max(count, maxLen);
            r += action[0];
            c += action[1];
        }
    }
}
